﻿namespace KeRui.DAL
{
    using KeRui.DBUtility;
    using KeRui.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class CmsComment
    {
        public int Add(KeRui.Model.CmsComment model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@AppID", SqlDbType.Int, 4), new SqlParameter("@ContentID", SqlDbType.Int, 4), new SqlParameter("@PublicID", SqlDbType.Int, 4), new SqlParameter("@UserID", SqlDbType.Int, 4), new SqlParameter("@Username", SqlDbType.NVarChar, 20), new SqlParameter("@City", SqlDbType.NVarChar, 50), new SqlParameter("@Content", SqlDbType.NVarChar, 0xff), new SqlParameter("@CreateIP", SqlDbType.VarChar, 15) };
            parameters[0].Value = model.AppID;
            parameters[1].Value = model.ContentID;
            parameters[2].Value = model.PublicID;
            parameters[3].Value = model.UserID;
            parameters[4].Value = model.Username;
            parameters[5].Value = model.City;
            parameters[6].Value = model.Content;
            parameters[7].Value = model.CreateIP;
            return DbHelperSQL.RunProcedure("CmsComment_ADD", parameters, out num);
        }

        public int AddBack(KeRui.Model.CmsComment model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@UserID", SqlDbType.Int, 4), new SqlParameter("@Username", SqlDbType.NVarChar, 20), new SqlParameter("@City", SqlDbType.NVarChar, 50), new SqlParameter("@Content", SqlDbType.NVarChar, 0xff), new SqlParameter("@QuoteID", SqlDbType.Int, 4), new SqlParameter("@CreateIP", SqlDbType.VarChar, 15), new SqlParameter("@QuoteContent", SqlDbType.VarChar) };
            parameters[0].Value = model.UserID;
            parameters[1].Value = model.Username;
            parameters[2].Value = model.City;
            parameters[3].Value = model.Content;
            parameters[4].Value = model.QuoteID;
            parameters[5].Value = model.CreateIP;
            parameters[6].Value = model.QuoteContent;
            return DbHelperSQL.RunProcedure("CmsComment_ADDForReply", parameters, out num);
        }

        public int AddNodeComment(KeRui.Model.CmsComment model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@AppID", SqlDbType.Int, 4), new SqlParameter("@ContentID", SqlDbType.Int, 4), new SqlParameter("@NodeID", SqlDbType.Int, 4), new SqlParameter("@PublicID", SqlDbType.Int, 4), new SqlParameter("@UserID", SqlDbType.Int, 4), new SqlParameter("@Username", SqlDbType.NVarChar, 20), new SqlParameter("@City", SqlDbType.NVarChar, 50), new SqlParameter("@Content", SqlDbType.NVarChar, 0xff), new SqlParameter("@CreateIP", SqlDbType.VarChar, 15) };
            parameters[0].Value = model.AppID;
            parameters[1].Value = model.ContentID;
            parameters[2].Value = model.NodeID;
            parameters[3].Value = model.PublicID;
            parameters[4].Value = model.UserID;
            parameters[5].Value = model.Username;
            parameters[6].Value = model.City;
            parameters[7].Value = model.Content;
            parameters[8].Value = model.CreateIP;
            return DbHelperSQL.RunProcedure("CmsComment_ADDNodeComment", parameters, out num);
        }

        public void Delete(int CommentID)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@CommentID", SqlDbType.Int, 4) };
            parameters[0].Value = CommentID;
            DbHelperSQL.RunProcedure("CmsComment_Delete", parameters, out num);
        }

        public KeRui.Model.CmsComment GetModel(int CommentID)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@CommentID", SqlDbType.Int, 4) };
            parameters[0].Value = CommentID;
            KeRui.Model.CmsComment comment = new KeRui.Model.CmsComment();
            DataSet set = DbHelperSQL.RunProcedure("CmsComment_GetModel", parameters, "ds");
            if (set.Tables[0].Rows.Count > 0)
            {
                if (set.Tables[0].Rows[0]["CommentID"].ToString() != "")
                {
                    comment.CommentID = int.Parse(set.Tables[0].Rows[0]["CommentID"].ToString());
                }
                if (set.Tables[0].Rows[0]["AppID"].ToString() != "")
                {
                    comment.AppID = int.Parse(set.Tables[0].Rows[0]["AppID"].ToString());
                }
                if (set.Tables[0].Rows[0]["ContentID"].ToString() != "")
                {
                    comment.ContentID = int.Parse(set.Tables[0].Rows[0]["ContentID"].ToString());
                }
                if (set.Tables[0].Rows[0]["NodeID"].ToString() != "")
                {
                    comment.NodeID = int.Parse(set.Tables[0].Rows[0]["NodeID"].ToString());
                }
                if (set.Tables[0].Rows[0]["SiteID"].ToString() != "")
                {
                    comment.SiteID = int.Parse(set.Tables[0].Rows[0]["SiteID"].ToString());
                }
                if (set.Tables[0].Rows[0]["PublicID"].ToString() != "")
                {
                    comment.PublicID = int.Parse(set.Tables[0].Rows[0]["PublicID"].ToString());
                }
                if (set.Tables[0].Rows[0]["UserID"].ToString() != "")
                {
                    comment.UserID = int.Parse(set.Tables[0].Rows[0]["UserID"].ToString());
                }
                comment.Username = set.Tables[0].Rows[0]["Username"].ToString();
                comment.City = set.Tables[0].Rows[0]["City"].ToString();
                comment.Content = set.Tables[0].Rows[0]["Content"].ToString();
                if (set.Tables[0].Rows[0]["SupportNum"].ToString() != "")
                {
                    comment.SupportNum = int.Parse(set.Tables[0].Rows[0]["SupportNum"].ToString());
                }
                if (set.Tables[0].Rows[0]["AgainstNum"].ToString() != "")
                {
                    comment.AgainstNum = int.Parse(set.Tables[0].Rows[0]["AgainstNum"].ToString());
                }
                if (set.Tables[0].Rows[0]["IsDelete"].ToString() != "")
                {
                    comment.IsDelete = int.Parse(set.Tables[0].Rows[0]["IsDelete"].ToString());
                }
                if (set.Tables[0].Rows[0]["floor"].ToString() != "")
                {
                    comment.floor = int.Parse(set.Tables[0].Rows[0]["floor"].ToString());
                }
                comment.QuoteID = set.Tables[0].Rows[0]["QuoteID"].ToString();
                comment.QuoteUser = set.Tables[0].Rows[0]["QuoteUser"].ToString();
                if (set.Tables[0].Rows[0]["QuoteTime"].ToString() != "")
                {
                    comment.QuoteTime = DateTime.Parse(set.Tables[0].Rows[0]["QuoteTime"].ToString());
                }
                comment.QuoteContent = set.Tables[0].Rows[0]["QuoteContent"].ToString();
                comment.CreateIP = set.Tables[0].Rows[0]["CreateIP"].ToString();
                if (set.Tables[0].Rows[0]["CreateTime"].ToString() != "")
                {
                    comment.CreateTime = DateTime.Parse(set.Tables[0].Rows[0]["CreateTime"].ToString());
                }
                return comment;
            }
            return null;
        }

        public void Remove(int CommentID)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@CommentID", SqlDbType.Int, 4) };
            parameters[0].Value = CommentID;
            DbHelperSQL.RunProcedure("CmsComment_DeleteByID", parameters, out num);
        }

        public int UpdateForSupportOrAgainst(int CommentID, int flag, string CreateIP)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@CommentID", SqlDbType.Int, 4), new SqlParameter("@flag", SqlDbType.Int, 4), new SqlParameter("@CreateIP", SqlDbType.VarChar, 50) };
            parameters[0].Value = CommentID;
            parameters[1].Value = flag;
            parameters[2].Value = CreateIP;
            return DbHelperSQL.RunProcedure("CmsComment_UpdateForSupportOrAgainst", parameters, out num);
        }
    }
}

